package _5thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-06-08 10:12
 */
public class CyclicBarrierTest {

    public static void main(String[] args) throws Exception{
        test2();

    }

    public static void test1(){

        Thread thread1=new Thread(){

            @Override
            public void run() {
                System.out.println("第1个人到了");
            }
        };

        Thread thread2=new Thread(){

            @Override
            public void run() {
                System.out.println("第2个人到了");
            }
        };

        Thread thread3=new Thread(){

            @Override
            public void run() {
                System.out.println("第3个人到了");
            }
        };

        Thread thread4=new Thread(){

            @Override
            public void run() {
                System.out.println("都到齐了,开始开会");
            }
        };

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }


    public static void test2(){

        CyclicBarrier cyclicBarrier=new CyclicBarrier(3,new Thread(){

            @Override
            public void run() {
                System.out.println("都到齐了,开始开会");

            }
        });
        Thread thread1=new Thread(){

            @Override
            public void run() {
                System.out.println("第1个人到了");
                try { cyclicBarrier.await(); } catch (Exception e) {}
                System.out.println("1发表言论");
            }
        };

        Thread thread2=new Thread(){

            @Override
            public void run() {
                System.out.println("第2个人到了");
                try { cyclicBarrier.await(); } catch (Exception e) {}
                System.out.println("2发表言论");
            }
        };

        Thread thread3=new Thread(){

            @Override
            public void run() {
                System.out.println("第3个人到了");
                try { cyclicBarrier.await(); } catch (Exception e) {}
                System.out.println("3发表言论");
            }
        };


        thread1.start();
        thread2.start();
        thread3.start();

    }
}
